﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClientRegister
{
    public partial class CreateClass : Form
    {
        string IDSub, IDTeacher;
        int Accept;
        string Room, StudyDay, Time;

        SynchronizeData sync = new SynchronizeData();
        GetNextID getId = new GetNextID();

        public CreateClass()
        {
            InitializeComponent();
        }

        private void CreateClass_Load(object sender, EventArgs e)
        {
            ////Lay du lieu tu server ve
            //sync.GetDataForInsertMethodWithID(sync.ClassTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.ClassTb, sync.updatecol);

            //Load danh sach cac mon hoc
            SubjectsDataProvider sub = new SubjectsDataProvider();
            DataSet dsSub = sub.LoadSubject();
            cbbxSubject.DataSource = dsSub.Tables[0];
            cbbxSubject.DisplayMember = "SubjectName";
            cbbxSubject.ValueMember = "ID";
            
            //Load danh sach phong hoc
            RoomDataProvider room = new RoomDataProvider();
            DataSet dsRoom = room.LoadRoom();
            cbbxRoom.DataSource = dsRoom.Tables[0];
            cbbxRoom.DisplayMember = "RoomName";
            cbbxRoom.ValueMember = "RoomName";
        }

        private void cbbxSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Lay ID mon hoc
            if (cbbxSubject.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                IDSub = cbbxSubject.SelectedValue.ToString();
                //Load danh sach Giao vien day mon hoc nay
                TeacherDataProvider teacher = new TeacherDataProvider();
                DataSet ds = teacher.LoadTeacher(IDSub);
                ccbxTeacher.DataSource = ds.Tables[0];
                ccbxTeacher.DisplayMember = "FullName";
                ccbxTeacher.ValueMember = "ID";
            }   
        }

        private void cbbxCalen_SelectedIndexChanged(object sender, EventArgs e)
        {
            StudyDay = cbbxCalen.SelectedItem.ToString();
        }

        private void cbbxTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            Time = cbbxTime.SelectedItem.ToString();
        }

        private void cbbxRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Hien thi dia chi khi chon room
            if (cbbxRoom.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                Room = cbbxRoom.SelectedValue.ToString();
            }
        }

        private void ccbxTeacher_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ccbxTeacher.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                IDTeacher = ccbxTeacher.SelectedValue.ToString();
            }
        }

        private void btnCreateClass_Click(object sender, EventArgs e)
        {
            ////Lay du lieu tu server ve
            //sync.GetDataForInsertMethodWithID(sync.ClassTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.ClassTb, sync.updatecol);

            string lastID = getId.getLastID(getId.classTB, getId.IDCol);
            string ID = getId.NextID(lastID, getId.prefixID);

            if(chkBoxStart.Checked == true)
                Accept = 1;
            else Accept = 0;
            ClassDataProvider create = new ClassDataProvider();
            int result = 0;
            
            result = create.AddClass(ID,txtName.Text, IDSub, IDTeacher, StudyDay, Room, Time, int.Parse(txtFee.Text.ToString()), txtStart.Text, txtEnd.Text, Accept);
            
            if (result != 0)
            {
                ////truyen du lieu len server
                //string xml = create.ConvertClassToXML(result.ToString());
                //sync.transfDataForInsertMethod(xml, sync.ClassTb);

                MessageBox.Show("Tạo lớp thành công");
            }
            else MessageBox.Show("Tạo lớp không thành công");

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
